package com.dk.service.impl;

import com.dk.mapper.RoleMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Role;
import com.dk.pojo.RoleMenuExample;
import com.dk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<Role> getInfo() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public Boolean insertInfo(Role role,Integer[] nodesIds) {

        //对role进行增加然后返回主键id

        //对中间表进行增加

        try {
            roleMapper.insertInfo(role) ;
            Integer rid = role.getRid();
            roleMenuMapper.insertMenuinfo(rid,nodesIds);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Integer> selectUserRoleByUid(Integer uid) {
        return roleMenuMapper.selectUserRoleByUid(uid);
    }

    @Override
    public List<Role> selectRole() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public Boolean insertInfo2(Role role, Integer[] nodesIds) {
        try {
            //通过rid修改role中的rname
            roleMapper.updateByPrimaryKeySelective(role);
            //通过rid删除中间表的mid
            //进行批量增加
            RoleMenuExample rm = new RoleMenuExample();
            RoleMenuExample.Criteria criteria = rm.createCriteria();
            criteria.andIdEqualTo(role.getRid());
            roleMenuMapper.deleteByExample(rm);
            roleMenuMapper.insertMenuinfo(role.getRid(),nodesIds);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Role selectById(Integer rid) {

        return roleMapper.selectByPrimaryKey(rid);
    }

    @Override
    public boolean deleteById(Integer id) {

      roleMenuMapper.deleteById(id);

      return roleMapper.deleteByPrimaryKey(id) > 0;


    }
}

